﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Services;
using DailyDiet.Services.Impl;
using DailyDiet.Services.Navigation;
using DailyDiet.util.eventArgs;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microcharts;
using SkiaSharp;
using Entry = Microcharts.ChartEntry;

namespace DailyDiet.ViewModels
{
    public class RecordViewModel : ViewModelBase {
        //构造函数 
        public RecordViewModel(
            IRecorderUtilService recorderUtilService,
            IDietRecorderStorageService dietRecorderStorageService) {
            _recorderUtilService = recorderUtilService;
            dietRecorderStorageService.DietRecorderStorageEvent += DietRecorderOnSet;

        }
        //工具类
        private IRecorderUtilService _recorderUtilService;


        //单日的entry列表
        private Entry[] _dailyEntries;

        public Entry[] DailyEntries
        {
            get => _dailyEntries;
            set => Set(nameof(DailyEntries), ref _dailyEntries, value);
        }


        //长期entry列表
        private Entry[] _longTermEntries;

        public Entry[] LongTermEntries {
            get => _longTermEntries;
            set => Set(nameof(LongTermEntries), ref _longTermEntries, value);
        }


        //每日的营养图
        private RadialGaugeChart _dailyRadarChart;

        public RadialGaugeChart DailyRadarChart {
            get => _dailyRadarChart;
            set => Set(nameof(DailyRadarChart), ref _dailyRadarChart, value);
        }

        //每周的营养图
        private LineChart _dailyRadarChart2;

        public LineChart DailyRadarChart2
        {
            get => _dailyRadarChart2;
            set => Set(nameof(DailyRadarChart2), ref _dailyRadarChart2, value);
        }


        //控件日历绑定的日期
        private DateTime _date;

        public DateTime Date {
            get => _date;
            set => Set(nameof(Date), ref _date, value);
        }


        //今日时间（在appear中初始化）
        private DateTime _nowTime;

        public DateTime NowTime
        {
            get => _nowTime;
            set => Set(nameof(NowTime), ref _nowTime, value);
        }

        //营养素的种类

        private string[] _properties = new string[] {
            "Protein", "Fat", "Sugars", "Cellulose", "Vitamin",
            "Carbohydrate"
        };

        public string[] Properties
        {
            get => _properties;
            set => Set(nameof(Properties), ref _properties, value);
        }

        private bool _isNew = true;




    //选择的营养素索引
    private int _selectProperty;

        public int SelectProperty {
            get => _selectProperty;
            set {
                Set(nameof(SelectProperty), ref _selectProperty, value);

                _ = RefreshCommandFunc(Date,RecorderUtilConstant.LongTermModelWeek,Properties[SelectProperty]);
            } 
        }

        //页面初始化
        private RelayCommand _appearingCommand;

        public RelayCommand AppearingCommand =>
            _appearingCommand ?? (_appearingCommand =
                new RelayCommand(async () => await AppearingCommandFunc(RecorderUtilConstant.LongTermModelWeek)));


        public async Task AppearingCommandFunc(string model) {
            if (!_isNew) {
                return;
            }
            NowTime = DateTime.Now;
            SelectProperty = 0;
            await RefreshCommandFunc(NowTime, model, Properties[SelectProperty]);
            _isNew = false;
        }
        //日期选择
        private RelayCommand _dateChosenCommand;

        public RelayCommand DateChosenCommand =>
            _dateChosenCommand ?? (_dateChosenCommand =
                new RelayCommand(async () => await DateChosenCommandFunc()));

        public async Task DateChosenCommandFunc()
        {
            await RefreshCommandFunc(Date,
                RecorderUtilConstant.LongTermModelWeek,
                Properties[SelectProperty]);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DietRecorderOnSet(object sender,
            DietRecorderStorageEventArgs e)
        {
            String mealTimes = e.MealTimes;
            DateTime dateTime = e.Time;
            if (dateTime == Date) {
                Task.Run(async () => await RefreshCommandFunc(Date,
                    RecorderUtilConstant.LongTermModelWeek,
                    Properties[SelectProperty]));
            }
        }

        //更新页面中更改的变量
        public async Task RefreshCommandFunc(DateTime dateTime, string model, string property)
        {
            
            
            var dietRecorders =
                    await _recorderUtilService.GetDietRecordersByDateAsync(dateTime);

            List<Diet> Dailydiets =
                await _recorderUtilService.ConvertRecordersIntoDietsAsync(dietRecorders);


            DailyEntries =
                _recorderUtilService.ConvertListIntoChartEntries(Dailydiets).ToArray();

            var DietList = await _recorderUtilService.GetLongTermDietRecorderAsync(dateTime,
                model);

            LongTermEntries = _recorderUtilService.ConvertDictIntoChartEntries(DietList,
                    property).ToArray();

            DailyRadarChart = new RadialGaugeChart() { Entries = DailyEntries ,MaxValue = 100, MinValue = 0};
            DailyRadarChart2 = new LineChart() { Entries = LongTermEntries };
        }

    }

}